﻿//---------------------------------------------------------------------------
#pragma hdrstop
#include "PositionClass.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
CPosition::CPosition(TCanvas* canvas, int x, int y) : CObject(canvas, x, y) {
	name = position_name + " " + IntToStr(namecount++);
	number = count++;
	description = "";
	markcount = 0;
	canvas->Font->Name = main_font;
	canvas->Font->Size = radius / 3 * 2;
}
//---------------------------------------------------------------------------
void CPosition::show() {
	double kx = position_scale[radius - 10][0];
	double ky = position_scale[radius - 10][1];
	canvas->Pen->Color = main_clBorder;
	canvas->Font->Color = main_clNFont;
	canvas->TextOutA(x - 5*radius / 3 - 2, y - 4*radius / 3 - 2, number);
	canvas->Font->Color = main_clFont;
	if (markcount == 0)
		canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
	if (markcount == 1) {
		canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
		canvas->Brush->Color = main_clBorder;
		canvas->Ellipse(x - radius / 3, y - radius / 3, x + radius / 3, y + radius / 3);
		canvas->Brush->Color = clWhite;
	}
	if (markcount >= 2 && markcount <= 9) {
		canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
		canvas->TextOutA(x - radius*kx, y - radius / 3*2 + ky, markcount);
	}
	if (markcount >= 10 && markcount <= 99) {
		canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
		canvas->TextOutA(x - 2*radius*kx, y - radius / 3*2 + ky, markcount);
	}
	if (markcount == 100) {
		canvas->Brush->Color = main_clNFont;
		canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
		canvas->Brush->Color = clWhite;
	}
}
//---------------------------------------------------------------------------
void CPosition::hide() {
	canvas->Pen->Color = clWhite;
	canvas->Font->Color = clWhite;
	canvas->TextOutA(x - 5*radius / 3 - 2, y - 4*radius / 3 - 2, number);
	canvas->Ellipse(x - radius, y - radius, x + radius, y + radius);
}
//---------------------------------------------------------------------------
bool CPosition::isSelect(int xm, int ym) {
	double rtec = sqrt((xm - x) * (xm - x) + (ym - y) * (ym - y));
	if (rtec <= radius)
		return true;
	else
		return false;
}
//---------------------------------------------------------------------------
void CPosition::setRadius(int radius) {
	if (radius < 10)
		radius = 10;
	if (radius > 19)
		radius = 19;
	this->radius = radius;
	canvas->Font->Size = radius / 3 * 2;
}
//---------------------------------------------------------------------------
void CPosition::setMarkCount(int markcount) {
	if (markcount > 99 && markcount != 100)
		markcount = 99;
	this->markcount = markcount;
}
//---------------------------------------------------------------------------
void CPosition::incMarkCount(int step) {
	if (markcount != 100) {
		markcount += step;
		if (markcount > 99)
			markcount = 99;
	}
}
//---------------------------------------------------------------------------
void CPosition::decMarkCount(int step) {
	if (markcount != 100) {
		markcount -= step;
		if (markcount < 0)
			markcount = 0;
	}
}
//---------------------------------------------------------------------------
int CPosition::getMarkCount() {
	return markcount;
}
//---------------------------------------------------------------------------
UnicodeString CPosition::save() {
	UnicodeString str = "", n = name, d = description;
	for (int i = 1; i <= n.Length(); i++)
		if (n[i] == ' ')
			n[i] = '`';
	for (int i = 1; i <= d.Length(); i++)
		if (d[i] == ' ')
			d[i] = '`';
	str += IntToStr(x) + " " + IntToStr(y) + " " + IntToStr(markcount) + " ";
	if (d == "")
		d = "EMPTY_DESCRIPTION";
	str += n + " " + d;
	return str;
}
//---------------------------------------------------------------------------
CPosition::~CPosition() {
	count--;
	if (count < 0)
		count = 0;
}
//---------------------------------------------------------------------------
